//
//  MasterViewController.m
//  MartketNote
//
//  Created by Nguyen Minh Giang on 12/11/13.
//  Copyright (c) 2013 Nguyen Minh Giang. All rights reserved.
//

#import "MasterViewController.h"

@interface MasterViewController ()

@end

@implementation MasterViewController

-(void)addClick
{
//    [self presentModalViewController:[[DetailViewController alloc] init] animated:YES];
    [self.navigationController pushViewController:[[DetailViewController alloc] init] animated:YES];
}

-(void)checkClick:(id)sender
{
    int index = [sender tag];
    NSLog(@"checked tag: %d", index);
    NSManagedObject *product = [productList objectAtIndex:index];
    [ReadWriteDataClass updateStatusProduct:product];
    [self.tableView reloadData];
}


- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    //load data
    productList = [ReadWriteDataClass getDataProducts];
    [self.tableView reloadData];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    self.title = @"Master";

    self.navigationItem.leftBarButtonItem = self.editButtonItem;
    
    UIBarButtonItem *btnAdd = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addClick)];
    self.navigationItem.rightBarButtonItem = btnAdd;
    
    
    
//    [ReadWriteDataClass insertNewProductWithID:0 name:@"Demo" detail:@"Demo detail" status:YES];
//    productList = [ReadWriteDataClass getDataProducts];
//    for(NSManagedObject *obj in productList)
//    {
//        NSLog(@"pro: %@ -- id: %d -- detail: %@", [obj valueForKey:ATT_PRODUCT], [[obj valueForKey:ATT_ID] intValue], [obj valueForKey:ATT_DETAIL]);
//    }
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return [productList count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    cell.backgroundColor = [UIColor clearColor];
    // Configure the cell...
    NSManagedObject *product = [productList objectAtIndex:indexPath.row];
    
    cell.textLabel.text = [NSString stringWithFormat:@"%d. %@", [[product valueForKey:ATT_ID] intValue], [product valueForKey:ATT_PRODUCT]];
    cell.detailTextLabel.text = [product valueForKey:ATT_DETAIL];
    
    //uibutton check
    BOOL isDone = [[product valueForKey:ATT_STATUS] boolValue];
    UIImage *imgChecked = [UIImage imageNamed:@"checked.png"];
    UIImage *imgUnCheck = [UIImage imageNamed:@"uncheck.png"];
    UIButton *btnCheck = [UIButton buttonWithType:UIButtonTypeCustom];
    btnCheck.frame = CGRectMake(tableView.frame.size.width - cell.frame.size.height, cell.frame.size.height/8, cell.frame.size.height*3/4, cell.frame.size.height*3/4);
    if(isDone)
        [btnCheck setBackgroundImage:imgChecked forState:UIControlStateNormal];
    else
        [btnCheck setBackgroundImage:imgUnCheck forState:UIControlStateNormal];
    btnCheck.tag = indexPath.row;
    [btnCheck addTarget:self action:@selector(checkClick:) forControlEvents:UIControlEventTouchUpInside];
    [cell addSubview:btnCheck];
    
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailViewController *detailController = [[DetailViewController alloc] init];
    detailController.product = [productList objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:detailController animated:YES];
}


// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}



// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [ReadWriteDataClass deleteManagedObject:[productList objectAtIndex:indexPath.row]];
        
        [productList removeObjectAtIndex:indexPath.row];
//        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        [tableView reloadData];
    }
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a story board-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}

 */

@end
